package com.vim.modules.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.model.response.Response;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.model.response.TableListResponse;
import com.vim.common.utils.ValidateUtils;
import com.vim.common.utils.WeixinTreeList;
import com.vim.common.utils.WeixinUtils;
import com.vim.modules.web.model.WeixinMenu;
import com.vim.modules.web.model.menubutton.ViewButton;
import com.vim.modules.web.service.WeixinMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/weixinMenu")
public class WeixinMenuController {

	@Resource
	private WeixinMenuService weixinMenuService;

	/**
	 * 列表查询
     * @param page 页码
     * @param limit 每页显示数量
	 */
	@RequestMapping("/list")
    public TableListResponse list(WeixinMenu weixinMenu, @RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotZero(page);
        ValidateUtils.checkNotZero(limit);
	    PageHelper.startPage(page,limit);
        List<WeixinMenu> list = weixinMenuService.findList(weixinMenu);
        PageInfo<WeixinMenu> pageInfo = new PageInfo<>(list);
        return new TableListResponse(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 菜单
     */
    @RequestMapping("/menuList")
    public RestfulResponse oneLevelMenuList(){
        Map<String, Object> result = new HashMap<>();
        List<WeixinMenu> list = weixinMenuService.findList(new WeixinMenu());
        WeixinTreeList tree = new WeixinTreeList(list);
        List<WeixinMenu> listTree = tree.buildTree();
        List<WeixinMenu> showListTree = tree.buildShowTree();
        result.put("listTree", listTree);
        result.put("showListTree", showListTree);
        return new RestfulResponse(result);
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestfulResponse edit(WeixinMenu weixinMenu){
        if(StringUtils.isNoneBlank(weixinMenu.getMenuId())){
            weixinMenuService.update(weixinMenu);
        }else{
            weixinMenuService.save(weixinMenu);
        }
        return new RestfulResponse(Response.OK);
    }

    /**
     * 详情
     * @param id 主键ID
     */
    @RequestMapping("/detail")
    public RestfulResponse detail(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        return new RestfulResponse(weixinMenuService.find(id));
    }

    /**
     * 删除
     * @param id 主键ID
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public RestfulResponse del(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        weixinMenuService.delete(id);
        return new RestfulResponse(Response.OK);
    }

    /**
     * 发布
     */
    @RequestMapping(value = "/deploy", method = RequestMethod.POST)
    public RestfulResponse deploy(){
        Map<String, Object> result = new HashMap<>();
        List<Object> buttonList = new ArrayList<>();

        List<WeixinMenu> list = weixinMenuService.findList(new WeixinMenu());
        WeixinTreeList tree = new WeixinTreeList(list);
        List<WeixinMenu> showListTree = tree.buildShowTree();
        for(WeixinMenu menu:showListTree){
            if(menu.getMenuList().size() > 0){
                Map<String, Object> subMenuMap = new HashMap<>();
                List<ViewButton> subMenuList = new ArrayList<>();
                for(WeixinMenu subMenu:menu.getMenuList()){
                    ViewButton button = new ViewButton();
                    button.setType("view");
                    button.setName(subMenu.getMenuName());
                    button.setUrl(subMenu.getMenuUrl());
                    subMenuList.add(button);
                }
                subMenuMap.put("name", menu.getMenuName());
                subMenuMap.put("sub_button", subMenuList);
                buttonList.add(subMenuMap);
            }else{
                ViewButton button = new ViewButton();
                button.setType("view");
                button.setName(menu.getMenuName());
                button.setUrl(menu.getMenuUrl());
                buttonList.add(button);
            }
        }
        result.put("button", buttonList);
        WeixinUtils.createMenuList(JSONObject.toJSONString(result));
        return new RestfulResponse(RestfulResponse.OK);
    }

}

